package com.jf3q.sup.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.sup.domain.Employees;
import com.jf3q.sup.domain.UserRoles;
import com.jf3q.sup.domain.Users;
import com.jf3q.sup.dto.EmpDto;
import com.jf3q.sup.dto.UserDto;
import com.jf3q.sup.service.*;
import com.jf3q.sup.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname EmpController
 * @Date 2024/3/25 19:19
 * @Created 叶超
 */
@RestController
@RequestMapping("/emp")
@CrossOrigin
public class EmpController {
    @Autowired
    EmployeesService employeesService;
    @Autowired
    UsersService usersService;
    @Autowired
    RolesService rolesService;
    @Autowired
    DepartmentsService departmentsService;
    @Autowired
    PositionsService positionsService;
    @Autowired
    UserRolesService userRolesService;

    @Autowired
    PasswordEncoder passwordEncoder;


    @GetMapping("/fenRole")
    public ResultVo fenRole(Integer userId,Integer... roleIds){
userRolesService.remove(new LambdaQueryWrapper<UserRoles>().eq(UserRoles::getUserId,userId));
        try {
            for (Integer roleId : roleIds) {
                UserRoles userRoles = new UserRoles();
                userRoles.setRoleId(roleId);
                userRoles.setUserId(userId);
                userRoles.setCreatedAt(new Date());
                userRoles.setUpdatedAt(new Date());
                userRolesService.save(userRoles);
            }
            return ResultVo.success("分配成功",null);
        } catch (Exception e) {
            throw new RuntimeException("分配失败");
        }

    }


    @GetMapping("/getRolesIds/{userId}")
    public ResultVo getRolesIds(@PathVariable Integer userId){
        List<UserRoles> list = userRolesService.list(new LambdaQueryWrapper<UserRoles>().eq(UserRoles::getUserId, userId));
        List<Integer> roleIds= new ArrayList<>();
        for (UserRoles userRoles : list) {
            roleIds.add(userRoles.getRoleId());
        }
        return ResultVo.success("",roleIds);
    }

    @GetMapping("/getPositions")
    public ResultVo  getPositions(){
        return ResultVo.success("",positionsService.list());
    }

    @GetMapping("/getDepts")
    public ResultVo getDepts(){
        return ResultVo.success("",departmentsService.list());
    }

    @GetMapping("/getRoles")
    public ResultVo getRoles(){
        return ResultVo.success("",rolesService.list());
    }

    @PutMapping("/update")
    @Transactional
    public ResultVo update(@RequestBody UserDto userDto) {
        Users byId = usersService.getById(userDto.getId());
        if(!userDto.getPassword().equals(byId.getPassword())){
            userDto.setPassword(passwordEncoder.encode(userDto.getPassword()));
        }
        return employeesService.updateEmp(userDto);
    }

    @PostMapping("/save")
    @Transactional
    public ResultVo save(@RequestBody UserDto userDto) {
       return employeesService.saveEmp(userDto);
    }

    @GetMapping("/remove/{userid}")
    @Transactional
    public ResultVo remove(@PathVariable Integer userid) {
        try {
            employeesService.remove(new LambdaQueryWrapper<Employees>().eq(Employees::getUserId,userid));
            usersService.removeById(userid);
            return ResultVo.success("删除成功",null);
        } catch (Exception e) {
            throw new RuntimeException("删除状态失败");
        }
    }
//    @GetMapping("/remove/{id}/{userId}")
//    @Transactional
//    public ResultVo remove(@PathVariable Integer id,@PathVariable Integer userId) {
//        try {
//            employeesService.remove(new LambdaQueryWrapper<Employees>().eq(Employees::getUserId,userId));
//            usersService.removeById(userId);
//            return ResultVo.success("删除成功",null);
//        } catch (Exception e) {
//            throw new RuntimeException("删除状态失败");
//        }
//    }

    @GetMapping("/updateStatus")
    public ResultVo updateStatus(Integer status,Integer id) {
        Users users = new Users();
        users.setId(id);
        users.setStatus(status);
        try {
            usersService.updateById(users);
            return ResultVo.success("修改状态成功",null);
        } catch (Exception e) {
            throw new RuntimeException("修改状态失败");
        }
    }

    @PostMapping("/getPage")
    public ResultVo getPage(@RequestBody EmpDto empDto) {
        try {
            Page<Employees> page = employeesService.getPage(empDto);
            return ResultVo.success("",page);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
